﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports System.Collections.Generic
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Xml

Namespace WillStrohl.Modules.Quiz

    Public NotInheritable Class QuizController
        Implements IPortable

#Region " Constants "

        Public Const SCRIPT_TAG_FORMAT As String = "<script language=""javascript"" type=""text/javascript"" src=""{0}""></script>"
        Public Const STYLESHEET_TAG_FORMAT As String = "<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />"

        Public Const QUERYSTRING_QUIZ_ID_KEY As String = "qi"
        Public Const QUERYSTRING_QUESTION_ID_KEY As String = "pi"
        Public Const QUERYSTRING_ANSWER_ID_KEY As String = "ai"
        Public Const QUERYSTRING_ACTION_KEY As String = "action"
        
#End Region

#Region " Data Access "

        Public Overloads Function AddQuiz(ByVal Quiz As QuizInfo) As Integer
            Return DataProvider.Instance().AddQuiz(Quiz.PortalId, Quiz.QuizName, Quiz.QuizDescription, Quiz.RandomizeQuestions, Quiz.QuestionCount, Quiz.BeginDate, Quiz.EndDate, Quiz.LastModifiedBy)
        End Function
        Public Overloads Function AddQuiz(ByVal PortalId As Integer, ByVal QuizName As String, ByVal QuizDescription As String, ByVal RandomizeQuestions As Boolean, ByVal QuestionCount As Integer, ByVal BeginDate As DateTime, ByVal EndDate As DateTime, ByVal LastModifiedBy As Integer) As Integer
            Return DataProvider.Instance().AddQuiz(PortalId, QuizName, QuizDescription, RandomizeQuestions, QuestionCount, BeginDate, EndDate, LastModifiedBy)
        End Function
        Public Overloads Sub UpdateQuiz(ByVal Quiz As QuizInfo)
            DataProvider.Instance().UpdateQuiz(Quiz.QuizId, Quiz.PortalId, Quiz.QuizName, Quiz.QuizDescription, Quiz.RandomizeQuestions, Quiz.QuestionCount, Quiz.BeginDate, Quiz.EndDate, Quiz.LastModifiedBy)
        End Sub
        Public Overloads Sub UpdateQuiz(ByVal QuizId As Integer, ByVal PortalId As Integer, ByVal QuizName As String, ByVal QuizDescription As String, ByVal RandomizeQuestions As Boolean, ByVal QuestionCount As Integer, ByVal BeginDate As DateTime, ByVal EndDate As DateTime, ByVal LastModifiedBy As Integer)
            DataProvider.Instance().UpdateQuiz(QuizId, PortalId, QuizName, QuizDescription, RandomizeQuestions, QuestionCount, BeginDate, EndDate, LastModifiedBy)
        End Sub
        Public Sub DeleteQuiz(ByVal QuizId As Integer)
            DataProvider.Instance().DeleteQuiz(QuizId)
        End Sub
        Public Function GetQuiz(ByVal QuizId As Integer) As QuizInfo
            Dim oQuiz As New QuizInfo
            oQuiz.Fill(DataProvider.Instance().GetQuiz(QuizId))
            Return oQuiz
        End Function
        Public Function GetQuizCount(ByVal PortalId As Integer) As Integer
            DataProvider.Instance().GetQuizCount(PortalId)
        End Function
        Public Function GetQuizzes(ByVal PortalId As Integer) As QuizInfoCollection
            Dim collQuiz As New QuizInfoCollection
            collQuiz.Fill(DataProvider.Instance().GetQuizzes(PortalId))
            Return collQuiz
        End Function


        Public Function GetQuestion(ByVal QuestionId As Integer) As QuestionInfo
            Dim oQuestion As New QuestionInfo
            oQuestion.Fill(DataProvider.Instance().GetQuestion(QuestionId))
            Return oQuestion
        End Function
        Public Function GetQuestionCount(ByVal QuizId As Integer) As Integer
            Return DataProvider.Instance().GetQuestionCount(QuizId)
        End Function
        Public Function GetQuestions(ByVal QuizId As Integer) As QuestionInfoCollection
            Dim collQuestion As New QuestionInfoCollection
            collQuestion.Fill(DataProvider.Instance().GetQuestions(QuizId))
            Return collQuestion
        End Function
        Public Overloads Function AddQuestion(ByVal Question As QuestionInfo) As Integer
            Return DataProvider.Instance().AddQuestion(Question.QuizId, Question.Question, Question.QuestionType, Question.RandomizeAnswers, Question.IsActive, Question.DisplayOrder, Question.LastModifiedBy)
        End Function
        Public Overloads Function AddQuestion(ByVal QuizId As Integer, ByVal Question As String, ByVal QuestionType As String, ByVal RandomizeAnswers As Boolean, ByVal IsActive As Boolean, ByVal DisplayOrder As Integer, ByVal LastModifiedBy As Integer) As Integer
            Return DataProvider.Instance().AddQuestion(QuizId, Question, QuestionType, RandomizeAnswers, IsActive, DisplayOrder, LastModifiedBy)
        End Function
        Public Overloads Sub UpdateQuestion(ByVal Question As QuestionInfo)
            DataProvider.Instance().UpdateQuestion(Question.QuestionId, Question.QuizId, Question.Question, Question.QuestionType, Question.RandomizeAnswers, Question.IsActive, Question.DisplayOrder, Question.LastModifiedBy)
        End Sub
        Public Overloads Sub UpdateQuestion(ByVal QuestionId As Integer, ByVal QuizId As Integer, ByVal Question As String, ByVal QuestionType As String, ByVal RandomizeAnswers As Boolean, ByVal IsActive As Boolean, ByVal DisplayOrder As Integer, ByVal LastModifiedBy As Integer)
            DataProvider.Instance().UpdateQuestion(QuestionId, QuizId, Question, QuestionType, RandomizeAnswers, IsActive, DisplayOrder, LastModifiedBy)
        End Sub
        Public Sub DeleteQuestion(ByVal QuestionId As Integer)
            DataProvider.Instance().DeleteQuestion(QuestionId)
        End Sub


        Public Function GetAnswer(ByVal AnswerId As Integer) As AnswerInfo
            Dim oAnswer As New AnswerInfo
            oAnswer.Fill(DataProvider.Instance().GetAnswer(AnswerId))
            Return oAnswer
        End Function
        Public Function GetAnswers(ByVal QuestionId As Integer) As AnswerInfoCollection
            Dim collAnswer As New AnswerInfoCollection
            collAnswer.Fill(DataProvider.Instance().GetAnswers(QuestionId))
            Return collAnswer
        End Function
        Public Overloads Function AddAnswer(ByVal Answer As AnswerInfo) As Integer
            Return DataProvider.Instance().AddAnswer(Answer.QuestionId, Answer.Answer, Answer.IsCorrect, Answer.IsActive, Answer.DisplayOrder, Answer.LastModifiedBy)
        End Function
        Public Overloads Function AddAnswer(ByVal QuestionId As Integer, ByVal Answer As String, ByVal IsCorrect As Boolean, ByVal IsActive As Boolean, ByVal DisplayOrder As Integer, ByVal LastModifiedBy As Integer) As Integer
            Return DataProvider.Instance().AddAnswer(QuestionId, Answer, IsCorrect, IsActive, DisplayOrder, LastModifiedBy)
        End Function
        Public Overloads Sub UpdateAnswer(ByVal Answer As AnswerInfo)
            DataProvider.Instance().UpdateAnswer(Answer.AnswerId, Answer.QuestionId, Answer.Answer, Answer.IsCorrect, Answer.IsActive, Answer.DisplayOrder, Answer.LastModifiedBy)
        End Sub
        Public Overloads Sub UpdateAnswer(ByVal AnswerId As Integer, ByVal QuestionId As Integer, ByVal Answer As String, ByVal IsCorrect As Boolean, ByVal IsActive As Boolean, ByVal DisplayOrder As Integer, ByVal LastModifiedBy As Integer)
            DataProvider.Instance().UpdateAnswer(AnswerId, QuestionId, Answer, IsCorrect, IsActive, DisplayOrder, LastModifiedBy)
        End Sub
        Public Sub DeleteAnswer(ByVal AnswerId As Integer)
            DataProvider.Instance().DeleteAnswer(AnswerId)
        End Sub

#End Region
        
#Region " IPortable Implementation "

        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements DotNetNuke.Entities.Modules.IPortable.ExportModule
            Dim sb As New StringBuilder(150)
            'Dim collInj As New LightboxInfoCollection
            'collInj = GetLightboxes(ModuleID)
            'Dim objSetting As New SettingInfo

            'sb.Append("<WillStrohl><Lightboxes>")
            'For Each obj As LightboxInfo In collInj
            '    sb.Append("<Lightbox>")

            '    sb.AppendFormat("<LightboxId>{0}</LightboxId>", obj.LightboxId)
            '    sb.AppendFormat("<ModuleId>{0}</ModuleId>", obj.ModuleId)
            '    sb.AppendFormat("<GalleryName><![CDATA[{0}]]></GalleryName>", XmlUtils.XMLEncode(obj.GalleryName))
            '    sb.AppendFormat("<GalleryDescription><![CDATA[{0}]]></GalleryDescription>", XmlUtils.XMLEncode(obj.GalleryDescription))
            '    sb.AppendFormat("<GalleryFolder><![CDATA[{0}]]></GalleryFolder>", XmlUtils.XMLEncode(obj.GalleryFolder))
            '    sb.AppendFormat("<DisplayOrder>{0}</DisplayOrder>", obj.DisplayOrder)
            '    ' 20101013 - Add HideTitleDescription
            '    sb.AppendFormat("<HideTitleDescription>{0}</HideTitleDescription>", obj.HideTitleDescription)

            '    objSetting = GetSettings(obj.LightboxId)
            '    sb.Append("<Settings>")
            '    sb.AppendFormat("<ChangeSpeed>{0}</ChangeSpeed>", objSetting.ChangeSpeed)
            '    sb.AppendFormat("<Cyclic>{0}</Cyclic>", objSetting.Cyclic)
            '    sb.AppendFormat("<EnableEscapeButton>{0}</EnableEscapeButton>", objSetting.EnableEscapeButton)
            '    sb.AppendFormat("<Margin>{0}</Margin>", objSetting.Margin)
            '    sb.AppendFormat("<Modal>{0}</Modal>", objSetting.Modal)
            '    sb.AppendFormat("<OnCancel>{0}</OnCancel>", XmlUtils.XMLEncode(objSetting.OnCancel))
            '    sb.AppendFormat("<OnCleanup>{0}</OnCleanup>", XmlUtils.XMLEncode(objSetting.OnCleanup))
            '    sb.AppendFormat("<OnClosed>{0}</OnClosed>", XmlUtils.XMLEncode(objSetting.OnClosed))
            '    sb.AppendFormat("<OnComplete>{0}</OnComplete>", XmlUtils.XMLEncode(objSetting.OnComplete))
            '    sb.AppendFormat("<OnStart>{0}</OnStart>", XmlUtils.XMLEncode(objSetting.OnStart))
            '    sb.AppendFormat("<Opacity>{0}</Opacity>", objSetting.Opacity)
            '    sb.AppendFormat("<OverlayColor>{0}</OverlayColor>", XmlUtils.XMLEncode(objSetting.OverlayColor))
            '    sb.AppendFormat("<OverlayOpacity>{0}</OverlayOpacity>", objSetting.OverlayOpacity)
            '    sb.AppendFormat("<OverlayShow>{0}</OverlayShow>", objSetting.OverlayShow)
            '    sb.AppendFormat("<Padding>{0}</Padding>", objSetting.Padding)
            '    sb.AppendFormat("<ShowCloseButton>{0}</ShowCloseButton>", objSetting.ShowCloseButton)
            '    sb.AppendFormat("<ShowNavArrows>{0}</ShowNavArrows>", objSetting.ShowNavArrows)
            '    sb.AppendFormat("<Speed>{0}</Speed>", objSetting.Speed)
            '    sb.AppendFormat("<TitlePosition>{0}</TitlePosition>", XmlUtils.XMLEncode(objSetting.TitlePosition))
            '    sb.AppendFormat("<TitleShow>{0}</TitleShow>", objSetting.TitleShow)
            '    sb.AppendFormat("<Transition>{0}</Transition>", XmlUtils.XMLEncode(objSetting.Transition))
            '    sb.Append("</Settings>")

            '    Dim collImage As ImageInfoCollection = GetImages(obj.LightboxId)
            '    sb.Append("<Images>")
            '    For Each img As ImageInfo In collImage
            '        sb.Append("<Image>")
            '        sb.AppendFormat("<ImageId>{0}</ImageId>", img.ImageId)
            '        sb.AppendFormat("<LightboxId>{0}</LightboxId>", img.LightboxId)
            '        sb.AppendFormat("<FileName>{0}</FileName>", XmlUtils.XMLEncode(img.FileName))
            '        sb.AppendFormat("<Title>{0}</Title>", XmlUtils.XMLEncode(img.Title))
            '        sb.AppendFormat("<Description>{0}</Description>", XmlUtils.XMLEncode(img.Description))
            '        sb.AppendFormat("<DisplayOrder>{0}</DisplayOrder>", img.DisplayOrder)
            '        sb.Append("</Image>")
            '    Next
            '    sb.Append("</Images>")

            '    sb.Append("</Lightbox>")
            'Next
            'sb.Append("</Lightboxes>")
            '' later on, will probably need to add module settings here
            'sb.Append("</WillStrohl>")

            Return sb.ToString
        End Function

        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserID As Integer) Implements DotNetNuke.Entities.Modules.IPortable.ImportModule
            'Dim injContents As XmlNode = DotNetNuke.Common.Globals.GetContent(Content, "Lightboxes")

            'For Each xmlLightbox As XmlNode In injContents.SelectNodes("Lightbox")
            '    Dim objLightbox As New LightboxInfo
            '    objLightbox.ModuleId = ModuleID
            '    objLightbox.GalleryName = xmlLightbox.SelectSingleNode("GalleryName").InnerText
            '    objLightbox.GalleryDescription = xmlLightbox.SelectSingleNode("GalleryDescription").InnerText
            '    objLightbox.GalleryFolder = xmlLightbox.SelectSingleNode("GalleryFolder").InnerText
            '    objLightbox.DisplayOrder = Integer.Parse(xmlLightbox.SelectSingleNode("DisplayOrder").InnerText, Globalization.NumberStyles.Integer)

            '    ' Backwards compatibility for any exported templates from previous versions
            '    Select Case Version
            '        Case "01.04.00"
            '            ' 20101013 - Added HideTitleDescription
            '            objLightbox.HideTitleDescription = Boolean.Parse(xmlLightbox.SelectSingleNode("HideTitleDescription").InnerText)
            '        Case Else
            '            objLightbox.HideTitleDescription = False ' Actually is unnecessary since the default is false
            '    End Select

            '    objLightbox.LightboxId = AddLightbox(objLightbox)

            '    For Each xmlSetting As XmlNode In xmlLightbox.SelectNodes("Settings")
            '        Dim objSetting As New SettingInfo
            '        objSetting.ChangeSpeed = Integer.Parse(xmlSetting.SelectSingleNode("ChangeSpeed").InnerText, Globalization.NumberStyles.Integer)
            '        objSetting.Cyclic = Boolean.Parse(xmlSetting.SelectSingleNode("Cyclic").InnerText)
            '        objSetting.EnableEscapeButton = Boolean.Parse(xmlSetting.SelectSingleNode("EnableEscapeButton").InnerText)
            '        objSetting.LightboxId = objLightbox.LightboxId
            '        objSetting.Margin = Integer.Parse(xmlSetting.SelectSingleNode("Margin").InnerText, Globalization.NumberStyles.Integer)
            '        objSetting.Modal = Boolean.Parse(xmlSetting.SelectSingleNode("Modal").InnerText)
            '        objSetting.OnCancel = xmlSetting.SelectSingleNode("OnCancel").InnerText
            '        objSetting.OnCleanup = xmlSetting.SelectSingleNode("OnCleanup").InnerText
            '        objSetting.OnClosed = xmlSetting.SelectSingleNode("OnClosed").InnerText
            '        objSetting.OnComplete = xmlSetting.SelectSingleNode("OnComplete").InnerText
            '        objSetting.OnStart = xmlSetting.SelectSingleNode("OnStart").InnerText
            '        objSetting.Opacity = Boolean.Parse(xmlSetting.SelectSingleNode("Opacity").InnerText)
            '        objSetting.OverlayColor = xmlSetting.SelectSingleNode("OverlayColor").InnerText
            '        objSetting.OverlayOpacity = Decimal.Parse(xmlSetting.SelectSingleNode("OverlayOpacity").InnerText, Globalization.NumberStyles.Float)
            '        objSetting.OverlayShow = Boolean.Parse(xmlSetting.SelectSingleNode("OverlayShow").InnerText)
            '        objSetting.Padding = Integer.Parse(xmlSetting.SelectSingleNode("Padding").InnerText, Globalization.NumberStyles.Integer)
            '        objSetting.ShowCloseButton = Boolean.Parse(xmlSetting.SelectSingleNode("ShowCloseButton").InnerText)
            '        objSetting.ShowNavArrows = Boolean.Parse(xmlSetting.SelectSingleNode("ShowNavArrows").InnerText)
            '        objSetting.Speed = Integer.Parse(xmlSetting.SelectSingleNode("Speed").InnerText, Globalization.NumberStyles.Integer)
            '        objSetting.TitlePosition = xmlSetting.SelectSingleNode("TitlePosition").InnerText
            '        objSetting.TitleShow = Boolean.Parse(xmlSetting.SelectSingleNode("TitleShow").InnerText)
            '        objSetting.Transition = xmlSetting.SelectSingleNode("Transition").InnerText

            '        AddSetting(objSetting)
            '    Next

            '    For Each xmlImage As XmlNode In xmlLightbox.SelectNodes("Images/Image")
            '        Dim objImage As New ImageInfo
            '        objImage.LightboxId = objLightbox.LightboxId
            '        objImage.FileName = xmlImage.SelectSingleNode("FileName").InnerText
            '        objImage.Title = xmlImage.SelectSingleNode("Title").InnerText
            '        objImage.Description = xmlImage.SelectSingleNode("Description").InnerText
            '        objImage.DisplayOrder = Integer.Parse(xmlImage.SelectSingleNode("DisplayOrder").InnerText, Globalization.NumberStyles.Integer)

            '        AddImage(objImage)
            '    Next

            'Next

        End Sub

#End Region

    End Class

End Namespace